package adv2;

public class Student implements Comparable<Student> {
	private String name;
	private String idcard;
	private int age;
	private String sex;

	public Student() {
	}

	public Student(String name, String idcard, int age, String sex) {
		super();
		this.name = name;
		this.idcard = idcard;
		this.age = age;
		setSex(sex);
		// this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIdcard() {
		return idcard;
	}

	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		if (sex.equals("男") || sex.equals("女")) {
			this.sex = sex;
			return;
		}
		throw new RuntimeException(sex + "不合法");
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((idcard == null) ? 0 : idcard.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (idcard == null) {
			if (other.idcard != null)
				return false;
		} else if (!idcard.equals(other.idcard))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "学生信息 [name=" + name + ", idcard=" + idcard + ", age=" + age
				+ ", sex=" + sex + "]";
	}

	@Override
	// 选一个比较的标准
	public int compareTo(Student o) {
		// this和o做比较
		// >0:this>o,<0:this<0
		// return this.age - o.age;
		return this.name.compareTo(o.name);
	}
}
